var uid = getParameter("uid");
var now = new Date();
var target;
var code=getParameter("code");
$(function() {
	if(uid){
	charts(now);
	}else{
		getToken(code);
	}
});
function getToken(code_){
	var urlStr = serverPath + "/server/doctor/diabetes/login.do";
	var jsonStr = 
	    '{'+
	    '	   "requestid":"",'+
	    '	   "requestsource":"diabetes",'+
	    '	   "sessionid":"",'+
	    '	   "uid":"",'+
	    '	   "method":"taipinglogin",'+
	    '	   "version":"1.0",'+
	    '	   "requestlist":'+
	    '	   ['+
	    '	      {'+
	    '	         "code":"'+code_+'"'+
	    '	      }'+
	    '	   ]'+
	    '	}';
	
	UseAjax(urlStr, jsonStr, document.cookie, getUid, '1');

};

function getUid(data) {
	if(data.resultcode=='0'){
		uid = data.uid;
		charts(now);
	}else{
		alert(data.resultmsg);	
	}
}

function charts(now) {
	var urlStr = serverPath + "/app/doctor/diabetes/sportView.do";
	/*var jsonStr = '{method:"showSportRecord",UId:"' + UId + '"}';*/
	var jsonStr = 
	    '{'+
	    '	   "requestid":"",'+
	    '	   "requestsource":"diabetes",'+
	    '	   "sessionid":"",'+
	    '	   "uid":"'+uid+'",'+
	    '	   "method":"showSportRecord",'+
	    '	   "version":"1.0",'+
	    '	   "requestlist":'+
	    '	   ['+
	    '	      {'+
	    '	         "uid":"'+uid+'"'+
	    '	      }'+
	    '	   ]'+
	    '	}';
	UseAjax(urlStr, jsonStr, document.cookie, rt, '1');
}
function rt(data) {
	target = 6000;
	var maxstep = 6000;
	var  datas = new Array();
	if(data.resultlist[0].records.length > 0 ){
		var records = data.resultlist[0].records;
		for (var int = 0; int < records.length; int++) {
			var stepnum = 0;
			if(records[int] != null){
				stepnum = Number(records[int].stepnum);
			}
			datas[int] = {'color' : getColor(stepnum),'y' : stepnum};
			if(stepnum>maxstep){maxstep=stepnum;}
		}
	}
	
	var today = getWeek(now);
	var week = [ "周一", "周二", "周三", "周四", "周五", "周六", "周日" ];
	
	var i = 0;
	for(var ie = 0;ie<week.length;ie++){
		if(today ==week[ie]){
			i=ie;
		}
	}
	var weeks = new Array();
	for (var int = 0; int < week.length; int++) {
		if (i <= 6) {
			weeks.push(week[i]);
			i++;
		} else {
			i = 0;
			weeks.push(week[i]);
			i++;
		}
	}
	$('#container')
			.highcharts(
					{
						chart : {
							type : 'column'
						},
						title : {
							text : ''
						},
						subtitle : {
							text : null
						},
						xAxis : {
							categories : weeks
						},
						yAxis : {
							tickInterval : parseInt(maxstep/6),
							min : 0,
							max : maxstep,
							// categories : steps,
							title : {
								enabled : false
							},
							labels : {
								formatter : function() {
									return "" + this.value + "步";
								}
							}
						},
						tooltip : {
							headerFormat : '<span style="font-size:10px">{point.key}</span><table>',
							pointFormat : '<tr><td style="color:{series.color};padding:0"> </td>'
									+ '<td style="padding:0"><b>{point.y:.1f} 步</b></td></tr>',
							footerFormat : '</table>',
							shared : true,
							useHTML : true
						},
						plotOptions : {
							column : {
								pointPadding : 0.2,
								borderWidth : 0
							}
						},
						credits : {
							enabled : false
						},
						legend : {
							enabled : false
						},
						series : [ {
							name : null,
							data : datas

						} ]
					});
}
function getColor(value) {
	var color;
	if (value < target) {
		color = "#E79C75";
	} else {
		color = "#CBE776";
	}
	return color;
}
function getWeek(date) {
	var week;
	switch (date.getDay()) {
	case 1:
		week = "周一";
		break;
	case 2:
		week = "周二";
		break;
	case 3:
		week = "周三";
		break;
	case 4:
		week = "周四";
		break;
	case 5:
		week = "周五";
		break;
	case 6:
		week = "周六";
		break;
	default:
		week = "周日";
	}
	return week;
}
